package com.qd.p2p.web.action.additional;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.AdditionalUserCommodityService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.AdditionalUserCommodityModel;

/**
 * Created by liaoxiangkai on 2015/12/11.
 */
public class UserCommodityAction extends BaseAction<AdditionalUserCommodityModel> implements ModelDriven<AdditionalUserCommodityModel> {

    @Resource
    private AdditionalUserCommodityService additionalUserCommodityService;

    private Map<String, Object> data;

    @Action(value = "/userCommodity/userCommodityList", interceptorRefs = {@InterceptorRef("session"), @InterceptorRef("globalStack") })
    public void userCommodityList() throws IOException {
        data = new HashMap<String, Object>();
        if (!checkParas()) return;
        PageDataList<AdditionalUserCommodityModel> modelList = additionalUserCommodityService.list(model);
        data.put("total", modelList.getPage().getTotal());
        data.put("data", modelList);
        printWebJson(getStringOfJpaObj(data));

    }

    /**
     * 检验用户及参数
     * @return
     * @throws IOException
     */
    private boolean checkParas() throws IOException {
        User user = getSessionUser();
        if (null == user) {
            printWebResult("请先登录!", false);
            return false;
        }
        model.setUser(user);

        String status = paramString("status");
        if (StringUtil.isBlank(status))
            model.setStatus(99);
        if (StringUtil.isBlank(paramString("rows")))
            model.setRows(5);
        return true;
    }


}
